package com.jhg.drgclinicalpathway;

import com.alibaba.fastjson.JSON;
import com.jhg.drgclinicalpathway.domain.RpnMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@ControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public void exceptionHandler(Throwable error, HttpServletResponse httpServletResponse) throws IOException {
        log.error("全局异常捕获：", error);
        String msg = error.getMessage();
        //if (error.getCause() != null) msg = error.getCause().getMessage();
        httpServletResponse.getWriter().write(JSON.toJSONString(RpnMsg.FAIL(msg)));
    }
}
